package com.jetpackmvp.utils

import android.text.TextUtils
import android.view.Gravity
import android.view.View
import android.widget.TextView
import android.widget.Toast
import com.jetpackmvp.R
import com.jetpackmvp.JetPackMvpApplication

/**
 * @author: GY.LEE
 * @date: 2019-06-06
 * @Des: 自定义 Toast 样式
 */
object ShowToast {
    private var toast: Toast? = null
    private var tvText: TextView? = null

    fun shortTime(resId: Int) {
        if (JetPackMvpApplication.application == null) {
            return
        }
        shortTime(
            JetPackMvpApplication.application.getString(
                resId
            )
        )
    }

    fun shortTime(text: String) {
        if (JetPackMvpApplication.application == null) {
            return
        }
        if (TextUtils.isEmpty(text)) {
            return
        }
        if (toast == null) {
            val view = View.inflate(JetPackMvpApplication.application, R.layout.layout_toast_body, null)
            tvText = view.findViewById<View>(R.id.tv_toast) as TextView
            tvText!!.text = text
            toast = Toast.makeText(
                JetPackMvpApplication.application, text,
                Toast.LENGTH_SHORT)
            toast!!.setGravity(Gravity.BOTTOM, 0,
                DisplayUtil.dp2px(120f)
            )
            toast!!.view = view
        } else {
            tvText!!.text = text
        }
        toast!!.show()
    }
}